package be.scond.cartpool;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.datastore.KeyFactory;

import be.scond.cartpool.API.GroupApi;
import be.scond.cartpool.model.Group;
import be.scond.cartpool.model.Order;
import be.scond.cartpool.model.ShoppingList;
import be.scond.cartpool.model.User;
import be.scond.utils.PMF;

public class CartpoolAPI_debugServlet extends HttpServlet {
	
	static final long serialVersionUID = 1;
	public static final String ACTION_RETURN_DEBUGLIST = "ACTION_RETURN_DEBUGLIST"; 
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String action = req.getParameter("action");
		//resp.setContentType("text/xml");
		resp.setContentType("text/plain");
				
		if(action.equals(ACTION_RETURN_DEBUGLIST)){
		    List<Group> groups = GroupApi.getAllGroups();
		    String debugList = "";
		    debugList += "<ul>";
		    for (Group g : groups) {
		    	debugList += "<li><a href='#' onclick=\"$.post('kmutolle', {action:'ACTION_DELETE_GROUP', name:'"+g.getName()+"'}, function(data){ onBtnUpdateDebugListClick(); });\">[delete]</a> "+g.getKey()+"&nbsp;-confirmed:"+g.isConfirmed()+"&nbsp; - name:"+g.getName()+"&nbsp; - pass:"+g.getPassword()+"&nbsp;</li>";
		    	debugList += "<ul style='padding-left:10px;'>";
				for(User u : g.getUsers()){
					debugList += "<li>saldo:" + u.getSaldo() + " key:" + u.getKey() + " : " + u.getName() + "</li>";
				}
				debugList += "<br/><ul style='padding-left:20px;'>";
				List<ShoppingList> shoppingLists = g.getShoppingLists();
				for (ShoppingList shoppingList : shoppingLists) {						
					PersistenceManager pm = PMF.get().getPersistenceManager();
					shoppingList = pm.getObjectById(ShoppingList.class, shoppingList.getKey());

					debugList += "<li><a href='#' onclick=\"$.post('kmutolle', {action:'ACTION_CLOSE_SHOPPINGLIST', email:$(\'#loginEmail\')[0].value, groupName:$(\'#loginGroupName\')[0].value, key:'"+KeyFactory.keyToString(shoppingList.getKey())+"'}, function(data){ onBtnUpdateDebugListClick(); });\">[close]</a> shoppingList: " + shoppingList.getKey() + " : " + shoppingList.getDate() + " : "+ shoppingList.getGroup().getName() + " closed: " + shoppingList.isClosed() + "</li>";
					debugList += "<ul style='padding-left:40px;'>";
					try {
						List<Order> orderList = shoppingList.getOrders(); 
						for(Order order : orderList){
							debugList += "<li><a href='#' onclick=\"$.post('kmutolle', {action:'ACTION_DELETE_ORDER', key:'"+order.getKey()+"'}, function(data){ onBtnUpdateDebugListClick(); });\">[delete]</a> userKey: " + order.getUserKey() + " order:" + order.getProduct() + " q:" + order.getQuantity() + " price: " + order.getPrice() + "</li>";
						}									
					} catch (Exception e) {
						debugList += "<li>no orders found</li>";
					}
					debugList += "</ul>";
				}
					debugList += "</ul>";
				debugList += "</ul>";
			}
		    debugList += "</ul>";
		    
		    PersistenceManager pm = PMF.get().getPersistenceManager();
		    pm.close();
			resp.getWriter().println(debugList);
		}
	}
}